#include <iostream>
using namespace std;


bool cac(int m){ return (m/1000 == (m/100)%10  ||  m/1000 == (m/10)%10  ||  m/1000 == m%10  ||  (m/100)%10 == (m/10)%10  ||  (m/100)%10 == m%10  ||  (m/10)%10 == m%10); }


int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int m;
		cin>>m;
		m++;
		while(cac(m))
			m++;
		cout<<m<<endl;
	}
	return 0;
}
